package cn.xuewei.controller;

import cn.xuewei.mapper.BannerMapper;
import cn.xuewei.pojo.dto.BannerInsetDTO;
import cn.xuewei.pojo.entity.Banner;
import cn.xuewei.pojo.vo.BannerAdminVO;
import cn.xuewei.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/v1/banners")
public class BannerController {
    @Autowired(required = false)
    BannerMapper mapper;

    /**
     * 轮播图展示
     * @return
     */
    @RequestMapping("")
    public ResultVO select(){
        return ResultVO.ok(mapper.select());
    }
    @RequestMapping("admin")
    public ResultVO admin(){
        List<BannerAdminVO> list = mapper.selectForAdmin();
        return ResultVO.ok(list);
    }

    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @RequestMapping("/{id}/delete")
    public ResultVO delete(@PathVariable int id){
        mapper.deleteById(id);
        return ResultVO.ok();
    }

    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @PostMapping("insertBanner")
    public ResultVO insertBanner(@RequestBody  BannerInsetDTO bannerInsetDTO){
        Banner banner = new Banner();
        banner.setImgUrl(bannerInsetDTO.getImgUrl());
        banner.setCreateTime(new Date());
        banner.setSort(bannerInsetDTO.getSort());
        mapper.insertBanner(banner);
        return ResultVO.ok();
    }
}
